
$.ajax({
    url:'/employee.do',
    method:'get',
    success:function(result){
        if(result&&result.length>0){
            for(var i=0;i<result.length;i++){
                var val = result[i];
                // if(val.state!=1){continue;}
                $(`<tr>
                <td><img src="/assets/img/${val.photo}"></td>
                <td>${val.id}</td>
                <td>${val.name}</td>
                <td>${val.sex}</td>
                <td>${val.age}</td>
                <td>${val.phone}</td>
                <td>${val.idCard}</td>
                <td>${val.role}</td>
                <td>
                    <a href="alter_empls.html?id=${val.id}" class="alter_btn am-btn am-btn-primary" style="border-radius:7%;box-shadow: rgb(75, 72, 72) 1px 1px 4px; font-size: 14px; margin:20px 5px 5px">修改</a>
                    <br>
                    <button myId=${val.id} class="del_btn am-btn am-btn-danger" style="border-radius:7%;box-shadow: rgb(75, 72, 72) 1px 1px 4px;font-size: 14px; margin:5px 5px 5px 0px">删除</button>
                </td>
            </tr>`).appendTo('tbody');
            }
        }
    }
});

//删除员工
$(document).on('click','.del_btn',function(){
    let id =$(this).attr('myId');
    swal({ 
        title: '确定删除吗？', 
        text: '你将无法恢复它！', 
        type: 'warning',
        showCancelButton: true, 
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定删除！', 
      }).then(function(result){
          if(!result.value){return;}
        $.ajax({
            url:'/delEmpl.do',
            method:'post',
            data:`id=${id}`,
            success:function(result){
                if(result==='success'){
                    swal('删除成功', '用户已被删除', 'success');
                    location.reload();
                }else{
                    swal('删除失败', '用户未被删除', 'error');
                }
            }
        });
      })
})

//添加员工
$(document).on('click','.add_btn',function(){
    location.href='/pages/alter_empls.html';
})